package com.colud.client.handler;


import com.colud.client.test.JT809ACKManager;
import com.colud.client.test.JT809Manager;
import com.colud.client.util.Constants;
import com.colud.client.util.JT809Constants;
import com.colud.client.vo.Message;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class CliBusiHandler extends SimpleChannelHandler {

    private static Logger log = LoggerFactory.getLogger(CliBusiHandler.class);

    @Autowired
    private JT809ACKManager jt809ACKManager;

    @Override
    public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        super.writeRequested(ctx, e);
    }


    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        Message msg = (Message) e.getMessage();
        log.info("应答-------------" + "0x" + Integer.toHexString(msg.getMsgId()));

        //-------登录相关应答  非登录不关心-------------/
        if (msg.getMsgId() == JT809Constants.UP_CONNECT_RSP) {
            ChannelBuffer msgBody = msg.getMsgBody();
            int result = msgBody.readByte();
            if (result == JT809Constants.UP_CONNECT_RSP_SUCCESS) {
                JT809Manager.LoginStatus = Constants.LOGIN_SUCCESS;
                log.info("--------------登录成功");
            } else {
                log.error("--------------登录异常，请检查:" + "0x0" + Integer.toHexString(result));
            }
        }

        ///********************************回应上级请求  发送数据***********************///*********//

        jt809ACKManager.ackSend(msg);

    }

}
